//
//  TMValueSetter.swift
//  ThemeManagement
//
//  Created by fanxiaoxin_1987@126.com on 06/20/2022.
//  Copyright (c) 2022 fanxiaoxin_1987@126.com. All rights reserved.
//

import UIKit

///主题设置器
@dynamicMemberLookup
public struct TMValueSetter<T: AnyObject> {
    ///设置目标
    public let object: T
    public init(object: T) {
        self.object = object
    }
    
    ///调用时执行，每次重设主题时执行
    public func load(_ action: @escaping (T) -> Void) {
        self.reload(action)
        action(self.object)
    }
    // 只有在重设主题时才执行
    public func reload(_ action: @escaping (T) -> Void) {
        TMThemeManager.shared.addObserve(self.object, for: action)
    }
    ///设置属性
    public subscript<Value>(dynamicMember kp: WritableKeyPath<T, Value?>) -> TMValue<Value>? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.load { (object) in
                    var obj = object
                    obj[keyPath: kp] = v.value()
                }
            }
        }
    }
    ///设置属性
    public subscript<Value>(dynamicMember kp: WritableKeyPath<T, Value>) -> TMValue<Value>? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.load { (object) in
                    if let val = v.value() {
                        var obj = object
                        obj[keyPath: kp] = val
                    }
                }
            }
        }
    }
}
extension TMValueSetter {
    ///将CGColor转为UIColor
    public subscript(dynamicMember kp: WritableKeyPath<T, CGColor?>) -> TMValue<UIColor>? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.load { (object) in
                    var obj = object
                    obj[keyPath: kp] = v.value()?.cgColor
                }
            }
        }
    }
    ///将CGColor转为UIColor
    public subscript(dynamicMember kp: WritableKeyPath<T, CGColor>) -> TMValue<UIColor>? {
        get {
            return nil
        }
        nonmutating  set {
            if let v = newValue {
                self.load { (object) in
                    if let val = v.value() {
                        var obj = object
                        obj[keyPath: kp] = val.cgColor
                    }
                }
            }
        }
    }
}
extension TMValueSetter where T: UIButton {
    public func setTitleColor(_ color: TMValue<UIColor>, for state: UIControl.State) {
        self.load { object in
            object.setTitleColor(color.value(), for: state)
        }
    }
    public func setImage(_ image: TMValue<UIImage>, for state: UIControl.State) {
        self.load { object in
            object.setImage(image.value(), for: state)
        }
    }
    public func setBackgroundImage(_ image: TMValue<UIImage>, for state: UIControl.State) {
        self.load { object in
            object.setBackgroundImage(image.value(), for: state)
        }
    }
}
